package com.example.eazytransfer.model;

import com.example.eazytransfer.Main;
import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Log {
    public static void write(String log) {
        try {
            File file = new File(Main.logFile);
            if (!file.exists()) {
                if (!file.getParentFile().exists())
                    file.getParentFile().mkdirs();
                file.createNewFile();
            }
            // 获取当前时间
            LocalDateTime now = LocalDateTime.now();
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 将当前时间转换为字符串
            String currentTime = now.format(formatter);
            log = currentTime + " : " + log;
            FileUtils.writeStringToFile(file, log, "UTF-8", true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
